package com.todo.member.application.convert;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.utils.TodoCommonUtils;
import com.todo.member.application.entity.bo.*;
import com.todo.member.application.entity.condition.MemberLevelConditionBO;
import com.todo.member.application.entity.condition.MemberMedalConditionBO;
import com.todo.member.common.enums.*;
import com.todo.member.infrastructure.entity.condition.MemberLevelInfoCondition;
import com.todo.member.infrastructure.entity.condition.MemberMedalInfoCondition;
import com.todo.member.infrastructure.entity.domain.MemberInfoDO;
import com.todo.member.infrastructure.entity.po.MemberInfoPO;
import com.todo.member.infrastructure.entity.po.MemberLevelInfoPO;
import com.todo.member.infrastructure.entity.po.MemberMedalInfoPO;
import com.todo.member.infrastructure.entity.po.MemberVipInfoPO;
import org.apache.commons.collections4.CollectionUtils;

import java.util.Date;
import java.util.stream.Collectors;

/**
 * @author azhebuxing
 * @date 2025/3/5 03:46
 * @description
 */
public class MemberInfoConvert {
    public static MemberInfoPO convertToMemberInfoPO(MemberInfoBO memberInfoBO) {
        MemberInfoPO memberInfoPO = new MemberInfoPO();
        memberInfoPO.setId(memberInfoBO.getId());
        memberInfoPO.setUserId(memberInfoBO.getUserId());
        memberInfoPO.setVipCode(memberInfoBO.getVipCode());
        memberInfoPO.setVipExpiredTime(memberInfoBO.getVipExpiredTime());
        memberInfoPO.setLevel(memberInfoBO.getLevel());
        memberInfoPO.setGrowthCount(memberInfoBO.getGrowthCount());
        memberInfoPO.setPointsCount(memberInfoBO.getPointsCount());
        memberInfoPO.setBonusCount(memberInfoBO.getBonusCount());
        memberInfoPO.setDisplayMedalCode(memberInfoBO.getDisplayMedalCode());
        memberInfoPO.setIsDelete(memberInfoBO.getIsDelete());
        memberInfoPO.setCreateTime(memberInfoBO.getCreateTime());
        memberInfoPO.setUpdateTime(memberInfoBO.getUpdateTime());
        return memberInfoPO;
    }

    public static MemberInfoBO convertToMemberInfoBO(MemberInfoPO memberInfoPO) {
        MemberInfoBO memberInfoBO = new MemberInfoBO();
        memberInfoBO.setId(memberInfoPO.getId());
        memberInfoBO.setUserId(memberInfoPO.getUserId());
        memberInfoBO.setVipCode(memberInfoPO.getVipCode());
        memberInfoBO.setVipVersion(memberInfoPO.getVipVersion());
        memberInfoBO.setVipExpiredTime(memberInfoPO.getVipExpiredTime());
        memberInfoBO.setLevel(memberInfoPO.getLevel());
        memberInfoBO.setGrowthCount(memberInfoPO.getGrowthCount());
        memberInfoBO.setPointsCount(memberInfoPO.getPointsCount());
        memberInfoBO.setBonusCount(memberInfoPO.getBonusCount());
        memberInfoBO.setDisplayMedalCode(memberInfoPO.getDisplayMedalCode());
        memberInfoBO.setIsDelete(memberInfoPO.getIsDelete());
        memberInfoBO.setCreateTime(memberInfoPO.getCreateTime());
        memberInfoBO.setUpdateTime(memberInfoPO.getUpdateTime());
        return memberInfoBO;
    }

    public static MemberDetailBO convertToMemberDetailBO(MemberInfoDO memberInfoDO) {
        if (memberInfoDO == null){
            return null;
        }
        MemberDetailBO memberDetailBO = new MemberDetailBO();
        MemberInfoBO memberInfoBO = new MemberInfoBO();
        memberInfoBO.setId(memberInfoDO.getId());
        memberInfoBO.setUserId(memberInfoDO.getUserId());
        memberInfoBO.setVipCode(memberInfoDO.getVipInfoDO().getVipCode());
        memberInfoBO.setVipVersion(memberInfoDO.getVipInfoDO().getVipVersion());
        memberInfoBO.setVipExpiredTime(memberInfoDO.getVipInfoDO().getVipExpiredTime());
        memberInfoBO.setLevel(memberInfoDO.getLevel());
        memberInfoBO.setGrowthCount(memberInfoDO.getGrowthCount());
        memberInfoBO.setPointsCount(memberInfoDO.getPointsCount());
        memberInfoBO.setBonusCount(memberInfoDO.getBonusCount());
        memberInfoBO.setDisplayMedalCode(memberInfoDO.getMedalInfoDO().getMedalCode());
        memberInfoBO.setIsDelete(memberInfoDO.getIsDelete());
        memberInfoBO.setCreateTime(memberInfoDO.getCreateTime());
        memberInfoBO.setUpdateTime(memberInfoDO.getUpdateTime());
        memberDetailBO.setMemberInfoBO(memberInfoBO);
        if (memberInfoDO.getVipInfoDO() != null){
            MemberDetailBO.VIPInfo vipInfo = new MemberDetailBO.VIPInfo();
            vipInfo.setName(memberInfoDO.getVipInfoDO().getName());
            vipInfo.setVipCode(memberInfoDO.getVipInfoDO().getVipCode());
            vipInfo.setDesc(memberInfoDO.getVipInfoDO().getDesc());
            vipInfo.setVipType(memberInfoDO.getVipInfoDO().getVipType());
            vipInfo.setVipExpiredTime(memberInfoDO.getVipInfoDO().getVipExpiredTime());
            memberDetailBO.setVipInfoBO(vipInfo);
        }
        if (memberInfoDO.getMedalInfoDO() != null){
            MemberDetailBO.MedalInfo medalInfo = new MemberDetailBO.MedalInfo();
            medalInfo.setMedalCode(memberInfoDO.getMedalInfoDO().getMedalCode());
            medalInfo.setTitle(memberInfoDO.getMedalInfoDO().getTitle());
            medalInfo.setDesc(memberInfoDO.getMedalInfoDO().getDesc());
            medalInfo.setMedalClassify(memberInfoDO.getMedalInfoDO().getMedalClassify());
            medalInfo.setCarryDisplayImg(memberInfoDO.getMedalInfoDO().getCarryDisplayImg());
            memberDetailBO.setMedalInfoBO(medalInfo);
        }
        return memberDetailBO;
    }

    public static MemberLevelInfoPO convertToMemberLevelInfoPO(MemberLevelBO memberLevelBO) {
        if (memberLevelBO == null){
            return null;
        }
        MemberLevelInfoPO memberLevelInfoPO = new MemberLevelInfoPO();
        memberLevelInfoPO.setId(memberLevelBO.getId());
        memberLevelInfoPO.setLevelName(memberLevelBO.getLevelName());
        memberLevelInfoPO.setLevelCode(memberLevelBO.getLevelCode());
        memberLevelInfoPO.setDesc(memberLevelBO.getDesc());
        memberLevelInfoPO.setUpgradeRule(memberLevelBO.getUpgradeRule());
        memberLevelInfoPO.setLevelPrivilege(memberLevelBO.getLevelPrivilege());
        memberLevelInfoPO.setLevelReward(JSONObject.toJSONString(memberLevelBO.getLevelReward()));
        memberLevelInfoPO.setLevel(memberLevelBO.getLevel());
        memberLevelInfoPO.setVersion(memberLevelBO.getVersion());
        memberLevelInfoPO.setStatus(memberLevelBO.getStatus().getType());
        memberLevelInfoPO.setIsDelete(0);
        memberLevelInfoPO.setCreateTime(new Date());
        memberLevelInfoPO.setUpdateTime(new Date());
        return memberLevelInfoPO;
    }

    public static MemberLevelBO convertToMemberLevelBO(MemberLevelInfoPO memberLevelInfoPO) {
        if (memberLevelInfoPO == null){
            return null;
        }
        MemberLevelBO memberLevelBO = new MemberLevelBO();
        memberLevelBO.setId(memberLevelInfoPO.getId());
        memberLevelBO.setLevelName(memberLevelInfoPO.getLevelName());
        memberLevelBO.setLevelCode(memberLevelInfoPO.getLevelCode());
        memberLevelBO.setDesc(memberLevelInfoPO.getDesc());
        memberLevelBO.setUpgradeRule(memberLevelInfoPO.getUpgradeRule());
        memberLevelBO.setLevelPrivilege(memberLevelInfoPO.getLevelPrivilege());
        memberLevelBO.setLevelReward(JSONObject.parseArray(memberLevelInfoPO.getLevelReward(), RelationRewardBO.class));
        memberLevelBO.setLevel(memberLevelInfoPO.getLevel());
        memberLevelBO.setVersion(memberLevelInfoPO.getVersion());
        memberLevelBO.setStatus(LevelStatusEnum.getByType(memberLevelInfoPO.getStatus()));
        return memberLevelBO;
    }

    public static MemberMedalInfoPO convertToMemberMedalInfoPO(MemberMedalInfoBO memberMedalInfoBO) {
        MemberMedalInfoPO memberMedalInfoPO = new MemberMedalInfoPO();
        memberMedalInfoPO.setId(memberMedalInfoBO.getId());
        memberMedalInfoPO.setMedalCode(memberMedalInfoBO.getMedalCode());
        memberMedalInfoPO.setTitle(memberMedalInfoBO.getTitle());
        memberMedalInfoPO.setDesc(memberMedalInfoBO.getDesc());
        memberMedalInfoPO.setMedalClassify(memberMedalInfoBO.getMedalClassify() == null ? null : memberMedalInfoBO.getMedalClassify().getType());
        memberMedalInfoPO.setRule(memberMedalInfoBO.getRule());
        memberMedalInfoPO.setUnacquiredDisplayImg(memberMedalInfoBO.getUnacquiredDisplayImg());
        memberMedalInfoPO.setAcquiredDisplayImg(memberMedalInfoBO.getAcquiredDisplayImg());
        memberMedalInfoPO.setCarryDisplayImg(memberMedalInfoBO.getCarryDisplayImg());
        memberMedalInfoPO.setSort(memberMedalInfoBO.getSort());
        memberMedalInfoPO.setStatus(memberMedalInfoBO.getStatus() == null ? null : memberMedalInfoBO.getStatus().getType());
        memberMedalInfoPO.setIsDelete(0);
        memberMedalInfoPO.setCreateTime(new Date());
        memberMedalInfoPO.setUpdateTime(new Date());
        return memberMedalInfoPO;
    }

    public static MemberMedalInfoBO convertToMemberMedalInfoBO(MemberMedalInfoPO memberMedalInfoPO) {
        MemberMedalInfoBO memberMedalInfoBO = new MemberMedalInfoBO();
        memberMedalInfoBO.setId(memberMedalInfoPO.getId());
        memberMedalInfoBO.setMedalCode(memberMedalInfoPO.getMedalCode());
        memberMedalInfoBO.setTitle(memberMedalInfoPO.getTitle());
        memberMedalInfoBO.setDesc(memberMedalInfoPO.getDesc());
        memberMedalInfoBO.setMedalClassify(MedalClassifyEnum.getByType(memberMedalInfoPO.getMedalClassify()));
        memberMedalInfoBO.setRule(memberMedalInfoPO.getRule());
        memberMedalInfoBO.setUnacquiredDisplayImg(memberMedalInfoPO.getUnacquiredDisplayImg());
        memberMedalInfoBO.setAcquiredDisplayImg(memberMedalInfoPO.getAcquiredDisplayImg());
        memberMedalInfoBO.setCarryDisplayImg(memberMedalInfoPO.getCarryDisplayImg());
        memberMedalInfoBO.setSort(memberMedalInfoPO.getSort());
        memberMedalInfoBO.setStatus(MedalStatusEnum.getByType(memberMedalInfoPO.getStatus()));
        return memberMedalInfoBO;
    }

    public static MemberMedalInfoCondition convertToMemberMedalInfoCondition(MemberMedalConditionBO memberMedalInfoBO) {
        if (memberMedalInfoBO == null){
            return null;
        }
        MemberMedalInfoCondition memberMedalInfoCondition = new MemberMedalInfoCondition();
        memberMedalInfoCondition.setId(memberMedalInfoBO.getId());
        memberMedalInfoCondition.setMedalCode(memberMedalInfoBO.getMedalCode());
        memberMedalInfoCondition.setTitle(memberMedalInfoBO.getTitle());
        memberMedalInfoCondition.setMedalClassify(memberMedalInfoBO.getMedalClassify() == null ? null : memberMedalInfoBO.getMedalClassify().getType());
        memberMedalInfoCondition.setStatus(memberMedalInfoBO.getStatus() == null ? null : memberMedalInfoBO.getStatus().getType());
        memberMedalInfoCondition.setPageIndex(memberMedalInfoBO.getPageNum());
        memberMedalInfoCondition.setPageSize(memberMedalInfoBO.getPageSize());
        return memberMedalInfoCondition;
    }

    public static PageResult<MemberMedalInfoBO> convertToPageMemberMedalInfoBO(PageResult<MemberMedalInfoPO> pageResult) {
        if (pageResult == null){
            return PageResult.of(Lists.newArrayList(), 0, 0, 0);
        }
        if (CollectionUtils.isEmpty(pageResult.getResults())) {
            return PageResult.of(Lists.newArrayList(), pageResult.getPageNum(), pageResult.getPageSize(), pageResult.getTotal());
        }
        PageResult<MemberMedalInfoBO> result = new PageResult<>();
        result.setCurrentIndex(pageResult.getCurrentIndex());
        result.setHasNext(pageResult.isHasNext());
        result.setPageCount(pageResult.getPageCount());
        result.setPageNum(pageResult.getPageNum());
        result.setPageSize(pageResult.getPageSize());
        result.setResults(pageResult.getResults().stream().map(MemberInfoConvert::convertToMemberMedalInfoBO).collect(Collectors.toList()));
        result.setTotal(pageResult.getTotal());
        return result;
    }

    public static MemberLevelInfoCondition convertToMemberLevelInfoCondition(MemberLevelConditionBO memberLevelConditionBO) {
        if (memberLevelConditionBO == null){
            return null;
        }
        MemberLevelInfoCondition memberLevelInfoCondition = new MemberLevelInfoCondition();
        memberLevelInfoCondition.setId(memberLevelConditionBO.getId());
        memberLevelInfoCondition.setLevelName(memberLevelConditionBO.getLevelName());
        memberLevelInfoCondition.setLevelCode(memberLevelConditionBO.getLevelCode());
        memberLevelInfoCondition.setLevel(memberLevelConditionBO.getLevel());
        memberLevelInfoCondition.setStatus(memberLevelConditionBO.getStatus() == null ? null : memberLevelConditionBO.getStatus().getType());
        memberLevelInfoCondition.setPageIndex(memberLevelConditionBO.getPageNum());
        memberLevelInfoCondition.setPageSize(memberLevelConditionBO.getPageSize());
        return memberLevelInfoCondition;
    }

    public static PageResult<MemberLevelBO> convertToMemberLevelBOPage(PageResult<MemberLevelInfoPO> pageResult) {
        if (pageResult == null){
            return PageResult.of(Lists.newArrayList(), 0, 0, 0);
        }
        if (CollectionUtils.isEmpty(pageResult.getResults())) {
            return PageResult.of(Lists.newArrayList(), pageResult.getPageNum(), pageResult.getPageSize(), pageResult.getTotal());
        }
        PageResult<MemberLevelBO> result = new PageResult<>();
        result.setCurrentIndex(pageResult.getCurrentIndex());
        result.setHasNext(pageResult.isHasNext());
        result.setPageCount(pageResult.getPageCount());
        result.setPageNum(pageResult.getPageNum());
        result.setPageSize(pageResult.getPageSize());
        result.setResults(pageResult.getResults().stream().map(MemberInfoConvert::convertToMemberLevelBO).collect(Collectors.toList()));
        result.setTotal(pageResult.getTotal());
        return result;
    }

    public static MemberVipInfoPO convertToMemberVipInfoPO(MemberVipInfoBO memberVipInfoBO) {
        if (memberVipInfoBO == null){
            return null;
        }
        MemberVipInfoPO memberVipInfoPO = new MemberVipInfoPO();
        memberVipInfoPO.setId(memberVipInfoBO.getId() == null ? TodoCommonUtils.buildLongGuid() : memberVipInfoBO.getId());
        memberVipInfoPO.setName(memberVipInfoBO.getName());
        memberVipInfoPO.setVipCode(memberVipInfoBO.getVipCode());
        memberVipInfoPO.setDesc(memberVipInfoBO.getDesc());
        memberVipInfoPO.setVipType(memberVipInfoBO.getVipType() == null ? null : memberVipInfoBO.getVipType().getType());
        memberVipInfoPO.setMarkingPrice(memberVipInfoBO.getMarkingPrice());
        memberVipInfoPO.setSellPrice(memberVipInfoBO.getSellPrice());
        memberVipInfoPO.setEffectiveDuration(memberVipInfoBO.getEffectiveDuration());
        memberVipInfoPO.setPurchaseInstructions(memberVipInfoBO.getPurchaseInstructions());
        memberVipInfoPO.setAfterSalesDescription(memberVipInfoBO.getAfterSalesDescription());
        memberVipInfoPO.setEquityStatement(memberVipInfoBO.getEquityStatement());
        memberVipInfoPO.setVipPrivilege(memberVipInfoBO.getVipPrivilege());
        memberVipInfoPO.setStatus(memberVipInfoBO.getStatus() == null ? null : memberVipInfoBO.getStatus().getType());
        memberVipInfoPO.setVersion(memberVipInfoBO.getVersion());
        memberVipInfoPO.setIsDelete(0);
        memberVipInfoPO.setCreateTime(memberVipInfoBO.getCreateTime());
        memberVipInfoPO.setUpdateTime(memberVipInfoBO.getUpdateTime());
        return memberVipInfoPO;
    }

    public static MemberVipInfoBO convertToMemberVipInfoBO(MemberVipInfoPO memberVipInfoPO) {
        if (memberVipInfoPO == null){
            return null;
        }
        MemberVipInfoBO memberVipInfoBO = new MemberVipInfoBO();
        memberVipInfoBO.setId(memberVipInfoPO.getId());
        memberVipInfoBO.setName(memberVipInfoPO.getName());
        memberVipInfoBO.setVipCode(memberVipInfoPO.getVipCode());
        memberVipInfoBO.setDesc(memberVipInfoPO.getDesc());
        memberVipInfoBO.setVipType(MemberVipTypeEnum.getByType(memberVipInfoPO.getVipType()));
        memberVipInfoBO.setMarkingPrice(memberVipInfoPO.getMarkingPrice());
        memberVipInfoBO.setSellPrice(memberVipInfoPO.getSellPrice());
        memberVipInfoBO.setEffectiveDuration(memberVipInfoPO.getEffectiveDuration());
        memberVipInfoBO.setPurchaseInstructions(memberVipInfoPO.getPurchaseInstructions());
        memberVipInfoBO.setAfterSalesDescription(memberVipInfoPO.getAfterSalesDescription());
        memberVipInfoBO.setEquityStatement(memberVipInfoPO.getEquityStatement());
        memberVipInfoBO.setVipPrivilege(memberVipInfoPO.getVipPrivilege());
        memberVipInfoBO.setStatus(MemberVipStatusEnum.getByType(memberVipInfoPO.getStatus()));
        memberVipInfoBO.setVersion(memberVipInfoPO.getVersion());
        memberVipInfoBO.setCreateTime(memberVipInfoPO.getCreateTime());
        memberVipInfoBO.setUpdateTime(memberVipInfoPO.getUpdateTime());
        return memberVipInfoBO;
    }
}
